package com.lhy.ltss.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lhy.ltss.common.UserRoleConst;
import com.lhy.ltss.entity.Book;
import com.lhy.ltss.exception.BookSearchException;
import com.lhy.ltss.service.BookService;
import com.lhy.ltss.service.OrderService;
import com.lhy.ltss.tag.HasRole;
import com.lhy.ltss.tag.MyLog;
import com.lhy.ltss.util.ResultJson;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.HashMap;

/**
 * @description:
 * 管理订单
 * @author: 李汇远
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    BookService bookService;

    @Autowired
    OrderService orderService;

    /**
     * 搜索满足where条件的书目，where条件将被用于http请求访问图书馆
     * @return
     */
    @GetMapping("book")
    public JSONObject search(@RequestParam HashMap<String, String> object) throws BookSearchException {
        return ResultJson.ok(bookService.search(object));
    }

    /**
     * 查询教师用户自己的未完成订单
     * @return
     */
    @GetMapping("self")
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject findTeacher(int page){
        return ResultJson.ok(orderService.findOrderByUsername(page));
    }

    /**
     * 查询教师已完成的订单
     * @param page
     * @return
     */
    @GetMapping("self-finish")
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject findTeacherFinish(int page){
        return ResultJson.ok(orderService.findHistoryOrderByUsername(page));
    }

    /**
     * 教师用户下单
     * @return
     */
    @PostMapping
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject makeOrder(String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = jsonObject.getJSONArray("books");
        orderService.makeOrder(jsonObject.getIntValue("id"), jsonArray.toJavaList(Book.class));
        return ResultJson.ok("下单成功");
    }

    /**
     * 统计教师订单的 未完成、配送、借阅次数
     * @return
     */
    @GetMapping("teacher-statistic")
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject countTeacherTodo(){
        return ResultJson.ok(orderService.statisticTeacherOrder());
    }

    /**
     * 管理员的统计信息
     *  待找书订单：
     * 待登记订单：
     * 待配送订单：
     * 待完成订单：
     * @return
     */
    @GetMapping("admin-statistic")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject adminStatistic(){
        return ResultJson.ok(orderService.statisticAdmin());
    }

    /**
     * 获取订单信息
     * @param username
     * @param status status in ('已完成'...)
     * @param page
     * @return
     */
    @GetMapping
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject findOrder(String username, String status,  Integer page){
        return ResultJson.ok(orderService.findOrder(username, status, page));
    }

    /**
     * 获取订单详情列表
     * @param id
     * @return
     */
    @GetMapping("detail")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject findOrderDetail(Integer id){
        return ResultJson.ok(orderService.findOrderDetail(id));
    }

    /**
     * 修改订单状态
     * @param id
     * @return
     */
    @PutMapping
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject updateOrderStatus(String status, Integer id){
        return ResultJson.ok(orderService.updateOrder(status, id));
    }

    /**
     * 更新订单详情的状态
     * @param status
     * @param id
     * @return
     */
    @PutMapping("detail")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject updateOrderDetailStatus(String status, Integer id){
        return ResultJson.ok(orderService.updateOrderDetail(status, id));
    }

    /**
     * 将待处理订单分配给找书员
     * @param id
     * @return
     */
    @PutMapping("findOrder")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject findOrder(Integer id){
        orderService.findOrder(id);
        return ResultJson.ok("分配完成");
    }

    /**
     * 登记订单
     * @param id
     * @return
     */
    @PutMapping("registerOrder")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject registerOrder(Integer id){
        orderService.registerOrder(id);
        return ResultJson.ok("登记成功") ;
    }

    /**
     * 确认配送的订单
     * @return
     */
    @PutMapping("distributeOrder")
    @MyLog("管理员确认配送的订单")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject distributeOrder(Integer id){
        orderService.distributeOrderCheck(id);
        return ResultJson.ok("确认成功");
    }



}
